#include "xmltree.h"

XMLTree::XMLTree()
{
    child=0;
}

XMLTree::~XMLTree(){
    delete keys;
    delete values;
}

XMLTree::XMLTree(XName name){
    parent=0;
    child=new ChildList();
    nodeName=name;
    keys=new KeyList();
    values=new ValuesList();
}

XMLTree::XMLTree(XName name, XValue value){    
    parent=0;
    child=new ChildList();
    nodeName=name;
    keys=new KeyList();
    keys->push_back(nodeName);
    values=new ValuesList();
    values->push_back(value);
}

XName XMLTree::rootNode() const{
    return nodeName;
}

//Add empty subtree.
void XMLTree::add(XName name){
//    XMLTree *subtree=new XMLTree(name);
//    subtree->parent=this;
//    child->push_back(subtree);
    add(name,"");
}

//Add element to current tree.
void XMLTree::add(XName name,XValue value){
    keys->push_back(name);
    values->push_back(value);
}

//Add tree.
void XMLTree::add(XMLTree &subtree){
    subtree.parent=this;
    child->push_back(&subtree);
}

ostream& operator << (ostream& outstream, const XMLTree& rhs){
    outstream<< XMLTag::Open(rhs.nodeName)<<endl;
    KeyList::iterator kit=rhs.keys->begin();
    ValuesList::iterator vit=rhs.values->begin();
    for (;kit!=rhs.keys->end() || vit!=rhs.values->end(); ++kit, ++vit){
        outstream<<" "<<XMLTag::Open(*kit)<< endl;
        outstream<<"  "<<*vit<<endl;
        outstream<<" "<<XMLTag::End(*kit)<<endl;
    }
    ChildList::iterator chit=rhs.child->begin();
    for (; chit!=rhs.child->end(); ++chit){
        outstream<<" ";
        operator<<(outstream,*(*chit));
    }
    outstream<<" ";
   outstream<< XMLTag::End(rhs.nodeName)<< endl;
    return outstream;
}
